package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.base.config.JwtUtils;
import com.atguigu.srb.commom.exception.Assert;
import com.atguigu.srb.commom.result.ResponseEnum;
import com.atguigu.srb.commom.untis.MD5;
import com.atguigu.srb.core.Vo.LoginVo;
import com.atguigu.srb.core.Vo.RegisterVO;
import com.atguigu.srb.core.Vo.UserInfoQuery;
import com.atguigu.srb.core.Vo.UserInfoVo;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.mapper.UserLoginRecordMapper;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserLoginRecord;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;


/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author lay_hu
 * @since 2021-12-13
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
   @Resource
    UserAccountMapper  userAccountMapper;
   @Resource
    UserLoginRecordMapper userLoginRecordMapper;
   @Transactional(rollbackFor = Exception.class)
    @Override
    public void register(RegisterVO registerVO) {
            //判断用户是否已经被注册
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
         wrapper.eq("mobile",registerVO.getMobile());
        Integer count = baseMapper.selectCount(wrapper);
        Assert.isTrue(count == 0, ResponseEnum.MOBILE_EXIST_ERROR);
        //给表插入数据
        UserInfo userInfo = new UserInfo();
        userInfo.setMobile(registerVO.getMobile());
        userInfo.setNickName(registerVO.getMobile());
        userInfo.setName(registerVO.getMobile());
        //密码加密用MD5算法
        userInfo.setPassword(MD5.encrypt(registerVO.getPassword()));
        userInfo.setStatus(UserInfo.STATUS_NORMAL);
        userInfo.setHeadImg(UserInfo.USER_AVATAR);
        baseMapper.insert(userInfo);
        UserAccount account = new UserAccount();
        account.setUserId(userInfo.getId());
        userAccountMapper.insert(account);


    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserInfoVo login(LoginVo loginVo, String ip) {
        String password = loginVo.getPassword();
        String mobile = loginVo.getMobile();
        Integer userType = loginVo.getUserType();
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile).eq("user_type",userType);
        UserInfo userInfo = baseMapper.selectOne(wrapper);
        System.out.println("userinfo"+userInfo);
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);
        Assert.equals(MD5.encrypt(password),userInfo.getPassword(),ResponseEnum.LOGIN_PASSWORD_ERROR);
        Assert.equals(userType,UserInfo.STATUS_NORMAL,ResponseEnum.LOGIN_LOKED_ERROR);
        UserLoginRecord userLoginRecord = new UserLoginRecord();
        userLoginRecord.setIp(ip);
        userLoginRecord.setUserId(userInfo.getId());
        userLoginRecordMapper.insert(userLoginRecord);
        //生成token 令牌
        String token = JwtUtils.createToken(userInfo.getId(), userInfo.getName());
        //封装userinfovo
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setName(userInfo.getName());
        userInfoVo.setHeadImg(userInfo.getHeadImg());
        userInfoVo.setMobile(mobile);
        userInfoVo.setToken(token);
        userInfoVo.setUserType(userType);
        userInfoVo.setNickName(userInfo.getNickName());
        return userInfoVo;


    }

    @Override
    public IPage<UserInfo> listPage(Page<UserInfo> userInfoPage, UserInfoQuery userInfoQuery) {
              if (userInfoQuery ==null){
                  Page<UserInfo> infoPage = baseMapper.selectPage(userInfoPage, null);
                  return infoPage;
              }
        String mobile = userInfoQuery.getMobile();
        Integer status = userInfoQuery.getStatus();
        Integer userType = userInfoQuery.getUserType();
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(mobile)){

        }
        //判断为空时也可以查询
        queryWrapper
                .eq(StringUtils.isNotBlank(mobile),"mobile",mobile)
                .eq(status !=null,"status",status)
                .eq(userType != null,"user_type",userType);
        Page<UserInfo> selectPage = baseMapper.selectPage(userInfoPage, queryWrapper);


        return selectPage ;
    }

    @Override
    public Boolean checkmobile(String mobile) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(queryWrapper);
        return integer>0;


    }
}
